package com.xingying.travel.service;



import com.xingying.travel.dao.ArticleDao;

import com.xingying.travel.pojo.Article;
import com.xingying.travel.util.IdWorker;
import com.xingying.travel.util.UploadUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ArticleService
{
    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private IdWorker idWorker;

    private final static String host="http://localhost:8080";

    public void add(Article article)
    {
        if(StringUtils.isEmpty(article.getId()))
        {
           article.setId(idWorker.nextId()+"" );
           if(article.getFile()!=null)
           {
               List<String> strings = UploadUtil.save(Arrays.asList(article.getFile()));
               article.setPicture(host+strings.get(0));
           }
            articleDao.saveAndFlush(article);
        }
    }

    public Page<Article> findSearch(Map searchMap, int page, int size)
    {
        Specification<Article> specification = createSpecification(searchMap);
        PageRequest pageRequest =  PageRequest.of(page-1, size);
        return articleDao.findAll(specification, pageRequest);
    }

    /**
     * 动态条件构建
     * @param searchMap
     * @return
     */
    private Specification<Article> createSpecification(Map searchMap) {

        return new Specification<Article>() {

            @Override
            public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                //
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                    predicateList.add(cb.equal(root.get("id"), searchMap.get("id")));
                }
                // 姓名
                if (searchMap.get("topic")!=null && !"".equals(searchMap.get("topic"))) {
                    predicateList.add(cb.like(root.get("topic"), "%"+(String)searchMap.get("topic")+"%"));
                }
                if (searchMap.get("createId")!=null && !"".equals(searchMap.get("createId"))) {
                    predicateList.add(cb.equal(root.get("createId"), searchMap.get("createId")));
                }
                if (searchMap.get("status")!=null && !"".equals(searchMap.get("status"))) {
                    predicateList.add(cb.equal(root.get("status"), searchMap.get("status")));
                }
                if (searchMap.get("articleType")!=null && !"".equals(searchMap.get("articleType"))) {
                    predicateList.add(cb.equal(root.get("articleType"), searchMap.get("articleType")));
                }
                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }

    public int findCount()
    {
       List<Article> articles =  articleDao.findByStatus((byte) 1);
       return articles.size();
    }

    public Object findAll()
    {
        return articleDao.findAll();
    }

    public void deleteById(String id)
    {
        articleDao.deleteById(id);
    }

    public void update(String id)
    {
        Article article = articleDao.getOne(id);
        if(article.getStatus()==(byte)0)
        {
            article.setStatus((byte) 1);
        }else{
            article.setStatus((byte)0);
        }
        articleDao.saveAndFlush(article);

    }
}
